<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\common\corelib\CacheHelper;
use app\models\PaperTest;
use app\models\Test;
use app\models\User;
use yii;


class TestLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getTest($paper_type,$query = [], $page = 1, $limit = 10)
    {
        if(isset($query['test_extend']) && $query['test_extend']==0)
            unset($query['test_extend']);
        $test = Test::find()
            ->select('t.*,pt.sort')
            ->from('{{%test}} t')
            ->leftJoin('{{%paper_test}} pt','pt.test_id=t.test_id')
            ->leftJoin('{{%paper}} p','p.paper_id=pt.paper_id')
            ->filterWhere($query)->andWhere("paper_type=$paper_type")->andWhere("t.is_del=0")->orderBy('pt.sort')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $test->asArray()->all();
        if($paper_type==1){
            $url = 'test/chiness';
        }elseif($paper_type==2){
            $url = 'test/spanish';
        }
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $test->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => $url,
                'query' => $query
            ]
        ];
        return $return;
    }

    public function getTestByPaper($paper_id,$query = [], $page = 1, $limit = 10){
        if(isset($query['paper_id'])){
            $paper_id = $query['paper_id'];
        }else{
            $query['paper_id'] = $paper_id;
        }

        $test = Test::find()
            ->select('t.*,pt.sort')
            ->from('{{%test}} t')
            ->leftJoin('{{%paper_test}} pt','pt.test_id=t.test_id')
            //->leftJoin('{{%test}} t','pt.test_id=t.test_id')
            ->filterWhere($query)->andWhere("pt.paper_id=$paper_id")->andWhere("t.is_del=0")->orderBy('pt.sort')
            ->offset(($page - 1) * $limit)->limit($limit);
        $lists = $test->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $test->count(),
                'page' => $page,
                'limit' => $limit,
                'url' => 'paper/edit',
                'query' => $query
            ]
        ];
        return $return;
    }

    public function addTest($params,$uploadresult=null,$test_option=null){
        $in_user = [];
        $user = User::find()->asArray()->all();
        foreach($user AS $key=>$value){
            $in_user[] = $value['open_id'];
        }
        CacheHelper::set($params['paper_id'].'_in_user',$in_user);
        $test = new Test();
        if($params['test_type']==1){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            if($params['paper_id']==1 || $params['paper_id']==2)
                $test->test_extend = $params['test_extend'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==2){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==3){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==4){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_extend = $params['test_extend'];
            $test->test_options = $uploadresult;
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==5){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            $test->test_extend = $params['test_extend'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==6){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==7){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            $test->test_answer = $uploadresult;
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==8){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==9){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            foreach ($params['test_options'] AS $key=>$value){
                if($value)
                    $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==10){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==11){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==12){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==13){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            $test->test_options = $test_option;
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==14){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            $test->test_extend = $test_option;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==15){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==16){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            foreach ($params['test_options'] AS $key=>$value){
                if($value)
                    $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==17){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                if($value)
                    $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==18){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                if($value)
                    $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==19){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==20){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $uploadresult;
            $test->test_extend = $params['test_extend'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==21){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            $test->test_extend = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==22){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }elseif($params['test_type']==23){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            $test->test_extend = $uploadresult;
            $test->test_options = $test_option;
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            $test->save();
            $paperTest = new PaperTest();
            $paperTest->paper_id = $params['paper_id'];
            $paperTest->test_id = $test->test_id;
            $paperTest->save();
            return true;
        }

    }

    public function editById($id,$params,$uploadresult=null,$test_option=null){
        $test = Test::findOne($id);
        if($test->test_type==1){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            if($params['paper_id']==1 || $params['paper_id']==2)
                $test->test_extend = $params['test_extend'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==2){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==3){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==4){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_extend = $params['test_extend'];
            $temp = json_decode($test->test_options,true);
            if(!isset($uploadresult['A']))
                $uploadresult['A'] = $temp['A'];
            if(!isset($uploadresult['B']))
                $uploadresult['B'] = $temp['B'];
            if(!isset($uploadresult['C']))
                $uploadresult['C'] = $temp['C'];
            if(!isset($uploadresult['D']))
                $uploadresult['D'] = $temp['D'];
            $test->test_options = json_encode($uploadresult);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==5){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            $test->test_extend = $params['test_extend'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==6){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==7){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            if($uploadresult)
                $test->test_answer = $uploadresult;
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==8){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==9){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            foreach ($params['test_options'] AS $key=>$value){
                if($value)
                    $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==10){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==11){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==12){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==13){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            $temp = json_decode($test->test_options,true);
            if(!isset($test_option['A']))
                $test_option['A'] = $temp['A'];
            if(!isset($test_option['B']))
                $test_option['B'] = $temp['B'];
            if(!isset($test_option['C']))
                $test_option['C'] = $temp['C'];
            if(!isset($test_option['D']))
                $test_option['D'] = $temp['D'];
            $test->test_options = json_encode($test_option);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==14){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            if($test_option)
                $test->test_extend = $test_option;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==15){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==16){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            foreach ($params['test_options'] AS $key=>$value){
                if($value)
                    $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==17){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                if($value)
                    $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==18){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                if($value)
                    $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==19){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==20){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            if($uploadresult)
                $test->test_detail = $uploadresult;
            $test->test_extend = $params['test_extend'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==21){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            if($uploadresult)
                $test->test_extend = $uploadresult;
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==22){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            foreach ($params['test_options'] AS $key=>$value){
                $test_options[$key] = $value;
            }
            $test->test_options = json_encode($test_options);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }elseif($test->test_type==23){
            $test->test_title = $params['test_title'];
            $test->test_type = $params['test_type'];
            $test->test_detail = $params['test_detail'];
            if($uploadresult)
                $test->test_extend = $uploadresult;
            $temp = json_decode($test->test_options,true);
            if(!isset($test_option['A']))
                $test_option['A'] = $temp['A'];
            if(!isset($test_option['B']))
                $test_option['B'] = $temp['B'];
            if(!isset($test_option['C']))
                $test_option['C'] = $temp['C'];
            if(!isset($test_option['D']))
                $test_option['D'] = $temp['D'];
            $test->test_options = json_encode($test_option);
            $test->test_answer = $params['test_answer'];
            //$test->paper_id = $params['paper_id'];
            $test->answer_option_num = $params['answer_option_num'];
            $test->add_time = date('Y-m-d H:i:s');
            return $test->save();
        }
    }

    public function getById($id){
        return Test::find()->where(['test_id'=>$id])->asArray()->one();
    }

    /*public function getPaperTestById($id){
        return PaperTest::find()->where([])
    }*/

    public function delete($id,$paper_id){
        /*if(!$ptest = PaperTest::findOne(['paper_id'=>$paper_id,'test_id'=>$id])) return false;
         $ptest->delete();
        $test = Test::findOne($id);
        $test->delete();
        return true;*/
        $in_user = [];
        $user = User::find()->asArray()->all();
        foreach($user AS $key=>$value){
            $in_user[] = $value['open_id'];
        }
        CacheHelper::set($paper_id.'_in_user',$in_user);
        if(!$test = Test::findOne($id)) return false;
        $test->is_del = 1;
        return $test->save();
    }

    public function sortTest($id,$paper_id,$sort){
        $in_user = [];
        $user = User::find()->asArray()->all();
        foreach($user AS $key=>$value){
            $in_user[] = $value['open_id'];
        }
        CacheHelper::set($paper_id.'_in_user',$in_user);
        if(!$test = PaperTest::findOne(['paper_id'=>$paper_id,'test_id'=>$id])) return false;
        $test->sort = $sort;
        return $test->save();
    }

}
